@using Passwordless.Common.Models.Reporting
@using Passwordless.AdminConsole.Components.Shared.ApexCharts.Models

<ApexChart Id="@Id" Options="_options" YAxisFormatting="LabelsFormattingTypes.Integer" />

@code {
    [Parameter]
    public required IEnumerable<PeriodicCredentialReportResponse> Data { get; set; }

    private const int MinDataPoints = 3;
    private const string Id = "total-users-count-chart";

    private ApexChartOptions<DateTime, int>? _options;
    
    protected override void OnInitialized()
    {
        if (Data.Count() < MinDataPoints)
        {
            Data = new List<PeriodicCredentialReportResponse>();
        }

        _options = new()
        {
            Chart = new Chart
            {
                Toolbar = new Toolbar
                {
                    Export = new Export()
                },
                Type = ChartTypes.Line
            },
            Series = new List<Series<int>>
            {
                new()
                {
                    Name = "Users",
                    Data = Data.Select(x => x.Users)
                }
            },
            Title = new Title
            {
                Text = "Total Users"
            },
            Xaxis = new()
            {
                Type = XAxisTypes.Datetime, 
                Categories = Data.Select(x => x.CreatedAt.ToDateTime(new TimeOnly(0, 0)))
            }
        };
        _options.Chart.Toolbar.Export.SetFilename(Id);
    }
}